require "analysis/AbstractAnalysis"
require "report/Report"

class AbstarctPhraseLevelAnalysis < AbstractAnalysis
  
  private
    
  def _pre_layer_global(directory, experimentalData, phraseCards)
  end
  
  def _post_layer_global(directory, experimentalData, phraseCards)
  end
  
  def _pre_subject_global(directory, experimentalData, phraseCards, layer, entries)
  end
  
  def _post_subject_global(directory, experimentalData, phraseCards, layer, entries)
  end
  
  # the core
  def _performance_analysis(report, uid, sbj_entries, experimentalData, phraseCards)
  end
  
  public  
  
  def analyze(directory, experimentalData, phraseCards)
    #create a directory
    Dir.mkdir(directory) unless File.directory?(directory)
    
    
    #get the data
    data = experimentalData.group_by_layer
    
    #call _pre_layer_global
    _pre_layer_global(directory, experimentalData, phraseCards)
    
    data.each_pair do |layer, entries|
      # create a directory for each layer
      dir = "#{directory}/#{layer}"
      Dir.mkdir(dir) unless File.directory?(dir)
      
      #group the entries by subject
      entries_by_subjects = {}
      entries.each do |e|
        #initialize if nil
        entries_by_subjects[e.uid] ||= []
        entries_by_subjects[e.uid] << e
      end
      
      #call _pre_subject_global
      _pre_subject_global(dir, experimentalData, phraseCards, layer, entries)      
           
      # for each subject
      entries_by_subjects.each_pair do |uid, sbj_entries|
        #create the report
        report = Report.new(dir, "Subject_#{uid}")
        
        #Call perform analysis
        _perform_analysis(report, uid, sbj_entries, experimentalData, phraseCards)  
        
        #generate report
        report.to_html
      end
      
      #call _post_subject_global
      _post_subject_global(dir, experimentalData, phraseCards, layer, entries)
       
    end
    
    #call _post_layer_global
    _post_layer_global(directory, experimentalData, phraseCards)
  end
 
  
end